package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.home.model.UserInfoBean;
import com.minyifang.wcspf.work.ui.user.model.UploadPicBean;
import com.minyifang.wcspf.work.ui.user.view.MemberInfoView;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/14.
 */
public class MemberInfoPresenter extends BasePresenter<MemberInfoView> {
    public void userInformation() {
        Map<String, Object> map = new HashMap<>();
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().userInformation(requestBody), respond -> {
            if (respond.result == 1) {
                String data = SafeUtils.decrypt(respond.data);
                LogUtil.e(data);
                UserInfoBean userInfoBean = new Gson().fromJson(data, UserInfoBean.class);
                getView().onGetUserInfo(userInfoBean);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }

    public void updateUserInformation(String saveHeadUrl, UserInfoBean userInfoBean) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", saveHeadUrl);
        map.put("nickname", userInfoBean.getNickname());
        map.put("realname", userInfoBean.getRealname());
        map.put("sex", userInfoBean.getSex());
        map.put("birthday", userInfoBean.getBirthday());
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().updateUserInformation(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().onUpdateUserInfoSucceed(respond.message);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }

    public void updateImg(File imgFile) {
        // 创建RequestBody，传入参数："multipart/form-data"，String
        RequestBody requestToken = RequestBody.create(MediaType.parse("multipart/form-data"), UserManager.getInstance().getToken());
        RequestBody requestClient = RequestBody.create(MediaType.parse("multipart/form-data"), "android");
        // 创建RequestBody，传入参数："multipart/form-data"，File
        RequestBody requestImgFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
        // 创建MultipartBody.Part，用于封装文件数据
        MultipartBody.Part requestImgPart = MultipartBody.Part.createFormData("files", imgFile.getName(), requestImgFile);
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().uploadImage(requestToken, requestClient, requestImgPart), new Consumer<HttpRespond<List<UploadPicBean>>>() {
            @Override
            public void accept(HttpRespond<List<UploadPicBean>> listHttpRespond) throws Exception {
                getView().hideLoadingView();
                if (listHttpRespond.result == 1) {
                    getView().onUploadPicSucceed(listHttpRespond.data);
                }
            }
        });
    }
}